package uk.co.leandro.views
{
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display3D.textures.Texture;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import uk.co.leandro.events.GameEvent;
	import uk.co.leandro.events.ScoreEvent;
	
	public class GameView extends MovieClip
	{
		[Embed(source="assets/BackGround.jpg")]
		private var BackGroundClass:Class;
		private var board:Board;
		private var boardMask:Shape;
		
		[Embed(source="assets/explosion.swf", symbol = "explosionAnimation")]
		private var ExplosionClass:Class;
		private var explosion:MovieClip = new ExplosionClass();
		
		private var countDownLabel:TextField;
		private var countDownText:TextField;
		
		private var scoreLabel:TextField;
		private var scoreText:TextField;
		private var format:TextFormat;
		private var score:int = 0;
		
		private var gameOverScreen:GameOverScreen;
		
		public function GameView()
		{
			this.addEventListener(Event.ADDED_TO_STAGE, addedToStage);
			var bg:Bitmap = new BackGroundClass();
			addChild(bg);
			
			explosion.x = 165;
			explosion.y = 355;
			addChild(explosion);
			
			format = new TextFormat();
			format.font="Verdana";
			format.size=24;
			
			countDownLabel = new TextField();
			countDownLabel.text = "Time:";
			countDownLabel.x = 50;
			countDownLabel.y = 120;
			countDownLabel.setTextFormat(format);
			addChild(countDownLabel);
			
			countDownText = new TextField();
			countDownText.text = "60";
			countDownText.x = countDownLabel.x + 100;
			countDownText.y = countDownLabel.y;
			countDownText.setTextFormat(format);
			addChild(countDownText);
			
			scoreLabel = new TextField();
			scoreLabel.text = "Score:";
			scoreLabel.x = countDownLabel.x;
			scoreLabel.y = countDownLabel.y + 50;
			scoreLabel.setTextFormat(format);
			addChild(scoreLabel);
			
			scoreText = new TextField();
			scoreText.text = "0";
			scoreText.x = scoreLabel.x + 100;
			scoreText.y = scoreLabel.y;
			scoreText.setTextFormat(format);
			addChild(scoreText);
			
			gameOverScreen = new GameOverScreen();
			gameOverScreen.addEventListener(GameOverScreen.PLAY_AGAIN, playAgainHandler);
		}
		
		private function addedToStage(event:Event):void
		{
			startRound();
		}
		
		private function addNewBoard():void
		{
			if (board != null)
			{
				board.removeEventListener(ScoreEvent.NEW_SCORE, newScoreHandler);
				removeChild(board);
			}
			board = new Board();
			board.x = 335;
			board.y = 110;
			addChild(board);
			score = 0;
			scoreText.text = score.toString();
			scoreText.setTextFormat(format);
			board.addEventListener(ScoreEvent.NEW_SCORE, newScoreHandler);
			
			if (boardMask == null)
			{
				boardMask = new Shape();
				boardMask.graphics.lineStyle(1, 0x000000);
				boardMask.graphics.beginFill(0xff0000); 
				boardMask.graphics.drawRect(board.x-20, board.y-20, board.width+40, board.height+40); 
				boardMask.graphics.endFill(); 
				addChild(boardMask);
			}
			board.mask = boardMask;
		}
		
		private function newScoreHandler(event:ScoreEvent):void
		{
			score += event.score;
			scoreText.text = score.toString();
			scoreText.setTextFormat(format);
		}
		
		public function startRound():void
		{
			addNewBoard()
			explosion.visible = true;
			explosion.gotoAndPlay(1);
			var e:GameEvent = new GameEvent(GameEvent.START);
			dispatchEvent(e);
		}
		
		private function stopRound():void
		{
			addChild(gameOverScreen);
			gameOverScreen.setScore(score);
			explosion.visible = false;
			var e:GameEvent = new GameEvent(GameEvent.STOP);
			dispatchEvent(e);
		}
		
		protected function playAgainHandler(event:Event):void
		{
			removeChild(gameOverScreen);
			startRound();
		}
		
		public function updateTime(countDown:int):void
		{
			//trace("countdown: "+countDown);
			countDownText.text = countDown.toString();
			countDownText.setTextFormat(format);
			if (countDown == 0)
			{
				stopRound();
			}
		}
	}
}